// NitroFileHeader.cs:
// Header for Nitro (Nintendo DS) files.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License.
// See LICENSE for more information.

using System;
using System.IO;
using System.Text;

namespace SharpNitroLib.NARC
{
	/// <summary>
	/// Header for Nitro (Nintendo DS) files.
	/// </summary>
    public class NitroFileHeader
    {
    	/// <summary>
    	/// FileType (magic bytes / identifier).
    	/// </summary>
        public UInt32 FileType { get; set; }
        
        
        /*
        /// <summary>
        /// Get FileType as String.
        /// </summary>
        public string FileTypeAsText
        {
            get
            {
                var bytes = BitConverter.GetBytes(FileType);
                return Encoding.ASCII.GetString(bytes);
            }
        }
        */
       
       /// <summary>
       /// Byte order magic.
       /// </summary>
        public UInt16 BOM { get; set; }
        /// <summary>
        /// TODO: Unknown.
        /// </summary>
        public UInt16 Unknown1 { get; set; }
        /// <summary>
        /// Size of file.
        /// </summary>
        public UInt32 FileSize { get; set; }
        /// <summary>
        /// Size of header.
        /// </summary>
        public UInt16 HeaderSize { get; set; }
        /// <summary>
        /// Number of frames.
        /// </summary>
        public UInt16 NumFrames { get; set; }

		/// <summary>
		/// Set values from reader.
		/// </summary>
		/// <param name="reader">Binary reader to use.</param>
        private void ReadInternal(BinaryReader reader)
        {
            FileType = reader.ReadUInt32();
            BOM = reader.ReadUInt16();
            Unknown1 = reader.ReadUInt16();
            FileSize = reader.ReadUInt32();
            HeaderSize = reader.ReadUInt16();
            NumFrames = reader.ReadUInt16();
        }

        /// <summary>
        /// Read NitroFileHeader from Binary reader. 
        /// </summary>
        /// <param name="reader">Binary reader to use.</param>
        /// <returns>NitroFileHeader from reader.</returns>
        public static NitroFileHeader Read(BinaryReader reader)
        {
            NitroFileHeader nitroFileHeader = new NitroFileHeader();
            nitroFileHeader.ReadInternal(reader);
            return nitroFileHeader;
        }

        /// <summary>
        /// Default byte order magic.
        /// </summary>
        public const UInt16 DEFAULT_BOM = 0xfffe;
        /// <summary>
        /// Default header size used in most cases.
        /// </summary>
        public const UInt16 DEFAULT_HEADERSIZE = 0x0010;
        /// <summary>
        /// Value of Unkown1.
        /// </summary>
        public const UInt16 DEFAULT_UNKNOWN1 = 0x0100;

        /// <summary>
        /// Create default NitroFileHeader.
        /// </summary>
        /// <returns></returns>
        public NitroFileHeader()
        {
            
		     FileType = NitroFileType.UNKN;
		     BOM = DEFAULT_BOM;
		     HeaderSize = DEFAULT_HEADERSIZE;
		     Unknown1 = DEFAULT_UNKNOWN1;

        }
    }
}